package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoFileDownloadInfo;
import com.ocom.accesservice.service.FileDownloadInfoService;
import com.ocom.common.entity.master.EntityFileDownloadInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FileDownloadInfoServiceImpl extends ServiceImpl<IDaoFileDownloadInfo, EntityFileDownloadInfo> implements FileDownloadInfoService {


    @Autowired
    private IDaoFileDownloadInfo fileDownloadInfo;

    /**
     * 更改文件状态 （/updateFileStatus)
     */
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public void UpdateFileStatus(EntityFileDownloadInfo entityFileDownloadInfo) {
        entityFileDownloadInfo.setUpdateTime(new Date());
        fileDownloadInfo.updateById(entityFileDownloadInfo);
    }

    /**
     * 添加文件下载申请 （/sendExcelApply）
     *
     * @return*/
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public Long SendExcelApply(EntityFileDownloadInfo entityFileDownloadInfo) {
        entityFileDownloadInfo.setCreateTime(new Date());
        entityFileDownloadInfo.setUpdateTime(new Date());
        fileDownloadInfo.insert(entityFileDownloadInfo);
        return entityFileDownloadInfo.getId();
    }

    /**
     * 获取列表 （/getDownLoadList）
     *
     * @return*/
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public List<EntityFileDownloadInfo> GetDownLoadList(Long pId) {
        QueryWrapper<EntityFileDownloadInfo> query = Wrappers.query();
        if (StringUtils.isNotNull(pId)){
            query.eq("p_id",pId);
        }
        query.orderByDesc("update_time");

        return fileDownloadInfo.selectList(query);
    }
}
